import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import  java.io.Serializable;
import java.util.ArrayList;

public class Colis implements Serializable{
	private int numero;
	private String intitule;
	public static ArrayList<Colis> lesColis = new ArrayList<Colis>();
	
	public Colis(int n, String i) {
		this.numero = n;
		this.intitule = i;
		
		lesColis.add(this);
	}
	
	public void finalize() {
		lesColis.remove(this);
	}
		
	public String toString() {
		return "{ numero : " + this.numero + " , intitule : " + this.intitule + " }";
	}
	
	public static void writeFile() {
		int nbElt = lesColis.size();

		ObjectOutputStream oos;

		try{

			oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("mesColis.txt"))));
			
			int idx = 0;
			while (idx < nbElt) {				
				oos.writeObject(lesColis.get(idx));
				idx++;
			}

			oos.close();

		}

		catch (java.io.IOException e) {e.printStackTrace();}
	}
	
	public static void readFile() {
		ObjectInputStream ois = null;

		try

		{

			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("mesColis.txt"))));	

			Colis colisToInsert = null;
			while ((colisToInsert = (Colis)ois.readObject()) != null) {				
				lesColis.add(colisToInsert);
			}

			ois.close();

		}		
		catch (EOFException eof) {}
		
		catch (IOException e) {e.printStackTrace();}

		catch (ClassNotFoundException e2) {e2.printStackTrace();}
	}
}
